let selectSchoolName = "";
let selectSchoolId = "";

var app = new Vue({
    el: '#addcampus',
    data() {
        return {
            mainimg : "",
        }
    },
    mounted: function () {
    },
    methods: {
    }
});
// 获取学校信息
function getSchoolList() {
    $.ajax({
        url: basePath + "/school/getSchoolList",
        contentType: "application/json;charset=UTF-8",
        type: "get",
        dataType: "json",
        complete: function () {
            layer.closeAll('loading');
        },
        success: function (response) {
            console.log("success")
            var data = response.data;
            var select = $('#schoolSelect');
            select.empty();  // 清空现有选项

            // 遍历数据，为每个学校创建一个 option 元素
            $.each(data, function(index, item) {
                select.append($('<option>', {
                    value: item.id,
                    text: item.name
                }));
            });

            // 选中第一个 option 元素
            select.find('option:first').attr('selected', 'selected');
            selectSchoolId = data[0].id;
            selectSchoolName = data[0].name;
            // 更新 Layui 的渲染
            layui.form.render('select');
        }, error: function () {
            console.log("error")
            console.log(data)
        }
    });
}

layui.use(['form', 'upload', 'element', 'layedit'], function () {
    let form = layui.form, layedit = layui.layedit;
    getSchoolList();

    //创建一个题目编辑器
    let editIndex = layedit.build('newscontents', {
        tool: [
            'strong'
            , 'italic'
            , 'underline'
            , 'del'
            , '|'
            , 'link'
            , 'face'
            , 'image'
        ]
    });
    // 获取选中的菜单
    form.on('select(setSchool)', function (data) {
        var indexGID = data.elem.selectedIndex;
        selectSchoolName = data.elem[indexGID].label;
        selectSchoolId = data.elem[indexGID].value;
    });

    form.on('submit(demo1)', function (data) {
        if (data.field.name.length > 200) {
            layer.msg('校区名称过长', {
                time: 1000,
                icon: 2,
                offset: '150px'
            });
            return false;
        }
        var object = new Object();
        object["name"] = data.field.name;
        object["description"] = data.field.description;
        object["schoolid"] = selectSchoolId;
        object["schoolname"] = selectSchoolName;
        console.log(object);
        var jsonData = JSON.stringify(object);

        $.ajax({
            url: basePath + "/campus/insert",
            data: jsonData,
            contentType: "application/json;charset=UTF-8",
            type: "post",
            dataType: "json",
            beforeSend: function () {
                layer.load(1, {
                    content: '发布中...',
                    success: function (layero) {
                        layero.find('.layui-layer-content').css({
                            'padding-top': '39px',
                            'width': '60px'
                        });
                    }
                });
            },
            complete: function () {
                layer.closeAll('loading');
            },
            success: function (data) {
                if(data.status===200){
                    layer.msg("新增成功", {
                        time: 1000,
                        icon: 1,
                        offset: '100px'
                    }, function () {
                        window.location.reload();
                    });
                }else {
                    layer.msg(data.message, {
                        time: 1000,
                        icon: 2,
                        offset: '50px'
                    });
                }
            }, error: function () {
                layer.msg('发布失败', {
                    time: 1000,
                    icon: 2,
                    offset: '150px'
                });
            }
        });
        return false;
    });
});